package org.jwcloud.project.module.zx.mongo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class MongoTemplateHelpImpl implements MongoTemplateHelp {

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void updateItemSpFlag(Integer prjId, Integer taskId, String spFlag) {
		// 修改要件为审批状态
		Query query = new Query();
		query.addCriteria(Criteria.where("prjId").is(prjId));
		query.addCriteria(Criteria.where("taskId").is(taskId));
		// query.addCriteria(Criteria.where("itemId").is(itemId));
		List<Map> list = mongoTemplate.find(query, Map.class, prjId.toString());
		for (Map map : list) {
			Map info = (Map) map.get("itemInfo");
			info.put("spFlag", spFlag);
			String infoId = (String) info.get("infoId");
			Criteria criteria = Criteria.where("itemInfo.infoId").is(infoId);
			if (null != criteria) {
				Query queryU = new Query(criteria);
				mongoTemplate.remove(queryU, prjId.toString());

				Map<String, Object> mapU = new HashMap<String, Object>();
				mapU.put("prjId", prjId);
				mapU.put("taskId", info.get("taskId"));
				mapU.put("itemId", info.get("itemId"));
				mapU.put("itemInfo", info);
				mongoTemplate.save(map, prjId.toString());
			}
		}
	}

}
